﻿
using Microsoft.EntityFrameworkCore;

await using var ctx = new BlogContext();
//var b = await ctx.Database.EnsureDeletedAsync();
//var c = await ctx.Database.EnsureCreatedAsync();

// Insert a Blog
//ctx.Blogs.Add(new() { Name = "FooBlog" });
//await ctx.SaveChangesAsync();

// Query all blogs who's name starts with F
//var fBlogs = await ctx.Blogs.Where(b => b.Name.StartsWith("F")).ToListAsync();

var blogs = await ctx.Blogs.ToListAsync();

foreach (var blog in blogs)
{
    Console.WriteLine(blog.Name);
}

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Username=postgres;Password=Icbc2222;Database=Mydb"); // wsl
                                                                                                                    //=> optionsBuilder.UseNpgsql(@"Host=localhost;Port=5433;Username=postgres;Password=Icbc3333;Database=Mydb"); // wsl 24.22
                                                                                                                    //=> optionsBuilder.UseNpgsql(@"Host=localhost;Port=8899;Username=postgres;Password=Icbc3333;Database=Mydb"); // socket fake service
}

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
}
/*
using Npgsql;

var connString = "Host=localhost;Username=postgres;Password=Icbc2222;Database=dvdrental";

await using var conn = new NpgsqlConnection(connString);
await conn.OpenAsync();


// Insert some data
await using (var cmd = new NpgsqlCommand("INSERT INTO data (some_field) VALUES (@p)", conn))
{
    cmd.Parameters.AddWithValue("p", "Hello world");
    await cmd.ExecuteNonQueryAsync();
}

// Retrieve all rows
await using (var cmd = new NpgsqlCommand("SELECT some_field FROM data", conn))
await using (var reader = await cmd.ExecuteReaderAsync())
{
    while (await reader.ReadAsync())
        Console.WriteLine(reader.GetString(0));
}

await Task.Delay(3000);

await conn.CloseAsync();
*/
